package com.sz.admin.chatmessage.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.sz.admin.chatmessage.service.ChatMessageService;
import com.sz.admin.chatmessage.pojo.po.ChatMessage;
import com.sz.admin.chatmessage.mapper.ChatMessageMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.sz.admin.chatmessage.pojo.dto.ChatMessageCreateDTO;
import com.sz.admin.chatmessage.pojo.dto.ChatMessageUpdateDTO;
import com.sz.admin.chatmessage.pojo.dto.ChatMessageListDTO;
import com.sz.admin.chatmessage.pojo.dto.ChatMessageImportDTO;
import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.admin.chatmessage.pojo.vo.ChatMessageVO;

/**
 * <p>
 * 私聊消息记录 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-06-30
 */
@Service
@RequiredArgsConstructor
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageMapper, ChatMessage> implements ChatMessageService {
    @Override
    public void create(ChatMessageCreateDTO dto){
        ChatMessage chatMessage = BeanCopyUtils.copy(dto, ChatMessage.class);
        save(chatMessage);
    }

    @Override
    public void update(ChatMessageUpdateDTO dto){
        ChatMessage chatMessage = BeanCopyUtils.copy(dto, ChatMessage.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(ChatMessage::getMessageId, dto.getMessageId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(chatMessage);
    }

    @Override
    public PageResult<ChatMessageVO> page(ChatMessageListDTO dto){
        Page<ChatMessageVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), ChatMessageVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<ChatMessageVO> list(ChatMessageListDTO dto){
        return listAs(buildQueryWrapper(dto), ChatMessageVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public ChatMessageVO detail(Object id){
        ChatMessage chatMessage = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(chatMessage);
        return BeanCopyUtils.copy(chatMessage, ChatMessageVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<ChatMessageImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), ChatMessageImportDTO.class, true);
        List<ChatMessageImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(ChatMessageListDTO dto, HttpServletResponse response) {
        List<ChatMessageVO> list = list(dto);
        String fileName = "私聊消息记录模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "私聊消息记录", ChatMessageVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(ChatMessageListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(ChatMessage.class);
        if (Utils.isNotNull(dto.getSenderId())) {
            wrapper.eq(ChatMessage::getSenderId, dto.getSenderId());
        }
        if (Utils.isNotNull(dto.getReceiverId())) {
            wrapper.eq(ChatMessage::getReceiverId, dto.getReceiverId());
        }
        if (Utils.isNotNull(dto.getIsDeleted())) {
            wrapper.eq(ChatMessage::getIsDeleted, dto.getIsDeleted());
        }
        return wrapper;
    }
}